package com.firefly.app.cordova.plugins;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import android.app.Activity;
import android.content.Intent;

import com.firefly.app.cordova.dto.MapDto;
import com.firefly.mobile.app.context.SytApplication;
import com.firefly.mobile.app.util.SytLog;

public class MapPlugin extends CordovaPlugin implements CordovaInterface{
	private final ExecutorService threadPool = Executors.newCachedThreadPool(); 
	 
    /** 
     * 注意 构造方法不能为 
     *  
     * HomePlugin(){}   
     * 可以不写或者 定义为如下 
     *   
     */  
    public MapPlugin() {  
    }  
    
  
    @Override  
    public boolean execute(final String action,final  org.json.JSONArray args,  
    		final  CallbackContext callbackContext) throws org.json.JSONException {  
        SytLog.i("mapPlugin", action);  
        
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                try {
					execPlugin(action, args, callbackContext);
				} catch (Exception e) {
					e.printStackTrace();
					SytLog.e("AdsPlugin", "插件调用出错");
					callbackContext.error("插件调用出错");
				}
            }
        });
        
        return true;  
  
    }  
    
    private void execPlugin(final String action,final  org.json.JSONArray args,  
    		final CallbackContext callbackContext) throws org.json.JSONException {
        try {
        	if (action.equals("isMapInstall")) {
        		callbackContext.success("{\"baidu\":"+isBaiduMapInstalled()+",\"gaode\":"+isGdMapInstalled()+"}"); 
		        return; 
			}
        	else if (action.equals("getCurrLocation")) {
        		//lat,lon,cityCode,proName,cityName,district
        		callbackContext.success("{\"lat\":"+SytApplication.lat+",\"lon\":"+SytApplication.lon+",\"cityCode\":"+SytApplication.cityCode+",\"proName\":\""+SytApplication.loProvince+"\",\"cityName\":\""+SytApplication.loCity+"\",\"district\":\""+SytApplication.district+"\"}"); 
		        return; 
			}
			if (action.equals("gotoMap")) {
				 // 获取JS传递的args的第一个参数  
				String bizInfo = args.getString(0);
			    // 获取JS传递的args的第一个参数  
			    SytLog.e("mapPlugin", "接收js调用"+action+"内容"+bizInfo); 
			    
				MapDto mapDto  = com.alibaba.fastjson.JSONObject.parseObject(bizInfo, MapDto.class);
				
				//移动APP调起Android百度地图方式举例
				//121.341191,31.130562
				Intent intent = Intent.getIntent("intent://map/marker?location=" +
//						"31.130562,121.341191" +
				mapDto.getLat() +","+ mapDto.getLng()+
						"&title=" +mapDto.getTitle()+
						"&content=" + mapDto.getAddress() +
						"&src=有限公司|com.firefly.mobile.app#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
				getActivity().startActivity(intent); //启动调用 
				 
				//网页应用调起Android百度地图方式举例
//				<a href="bdapp://map/marker?location=40.047669,116.313082&title=我的位置&content=百度奎科大厦&src=yourCompanyName|yourAppName">地图标点</a>
				
				
				
		    	callbackContext.success("00"); 
		        return; 
			}
		} catch (Exception e) {
			callbackContext.success(""); 
			e.printStackTrace();
			SytLog.e("mapPlugin", "调用地图信息出错");
		}  
    }
    
    
    /**
     * 地图应用是否安装
     * @return
     */
    public static boolean isGdMapInstalled(){
        return isInstallPackage("com.autonavi.minimap");
    }

    public static boolean isBaiduMapInstalled(){
        return isInstallPackage("com.baidu.BaiduMap");
    }

    private static boolean isInstallPackage(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }
    
	@Override
	public void startActivityForResult(CordovaPlugin command,
			Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setActivityResultCallback(CordovaPlugin plugin) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Activity getActivity() {
		// TODO Auto-generated method stub
		return this.cordova.getActivity();
	}

	@Override
	public ExecutorService getThreadPool() {
		return threadPool;
	}
  
}
